<?php

namespace app\api\controller;
use app\common\model\shop\Order;
use app\common\model\shop\ShopUser;
use app\models\User;
use think\Db;
use app\common\controller\Api;
/**
 * 添加信息提醒
 * /api/msg_reminder/index
 */
class MsgReminder extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    /**
     * 任务
     */
    public function index()
    {
        //6=考核提醒,
        $this->assessmentMsg();
    }
    /**
     * Notes:6=考核提醒 查询hjmall_order表 每天提示一次 多条信息只查询一条limit1
     * User: szh
     * Date: 2021/1/8
     * @param int $page
     */
    public function assessmentMsg($page=1)
    {
        $user = new ShopUser();
        $list = $user->field(['id','username','nickname'])->page($page,200)->select();
        foreach ($list as $k => $v)
        {
            $order = new Order();
            $orderInfo = $order->where([
                            "is_examination" => 2,
                            "is_examination_win" => 1,
                            "user_id" => $v['id'],
                        ])->order('id desc')->find();
            if($orderInfo){
                $redisKey = "msg_reminder:assessment_msg_".date("Ymd")."order_id_".$orderInfo['id'];
                $user_ids = $this->checkDiff($redisKey, $v['id'], 259200);
                if($user_ids){
                    //投诉反馈回复
                    $m_msg = new \app\common\model\shop\MsgReminder();
                    $title = "您已下单的产品须通过考核才能发货,请及时前往考试";
                    $content = "您已下单的产品须通过考核才能发货,请及时前往考试,订单编号《".$orderInfo['order_no']."》";
                    $m_msg->addMsg(6,$title,$content, $orderInfo['id'], $user_ids);
                }
            }
        }
        $count = $user->count();
        $pageSize = ceil($count/200);
        if($page < $pageSize){
            $page++;
            $this->assessmentMsg($page);
        }
        return;
    }


    /**
     * Notes: 检查 新的userIds中还有哪些未发通知 合并存储并返回
     * User: szh
     * Date: 2021/1/7
     * @param $redisKey  redisKey
     * @param $userIds  要发信息的ids
     * @param $expireTime  redis延长的时间
     */
    public function checkDiff($redisKey,$userIds,$expireTime=0)
    {
        $userIds = is_array($userIds)? $userIds: [$userIds];
        $redisModel = $this->getRedis();
        if($old_ids = $redisModel->hGetAll($redisKey)){
            $diff_ids = array_filter($userIds,function($v) use ($old_ids){
                return !in_array($v,$old_ids);
            });
            $redisModel->hMSet($redisKey, array_unique(array_merge($old_ids?:[], $userIds?:[])));
        }else{
            $redisModel->hMSet($redisKey, $userIds);
            $redisModel->expire($redisKey, 86400*3);
            $diff_ids = $userIds;
        }
        if($expireTime > 0){
            $redisModel->expire($redisKey, $expireTime);
        }
        return $diff_ids;
    }

    //获取redis对象
    public function getRedis()
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1',6379);
        return $redis;
    }
}
